﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 25/04/2014
 * Time: 06:51 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
	/// <summary>
	/// Description of notadaimpl.
	/// </summary>
	public class notadaimpl:da.notada
	{
		public notadaimpl()
		{
		}
		
		public bool SaveNota(int alu_dni, int mat_id, int cur_id, int tie_id, DateTime fecha, string valor)
		{
			bool ingresada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "INSERT INTO not_notas(alu_dni, mat_id, cur_id, tie_id, not_fecha, not_valor)" +
                    			"VALUES(@alu_dni, @mat_id, @cur_id, @tie_id, @not_fecha, @valor)";
					
					OleDbCommand insertar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					insertar.Parameters.AddWithValue("@alu_dni",alu_dni);
					insertar.Parameters.AddWithValue("@mat_id",mat_id);
					insertar.Parameters.AddWithValue("@cur_id",cur_id);
					insertar.Parameters.AddWithValue("@tie_id",tie_id);
					insertar.Parameters.AddWithValue("@not_fecha",fecha);
					insertar.Parameters.AddWithValue("@valor",valor);
					
					insertar.ExecuteNonQuery();
					
					trans.Commit();
					
					ingresada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				ingresada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return ingresada;
		}
		
		public bool DeleteNota(int alu_dni, int mat_id, DateTime fecha)
		{
			bool eliminada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "DELETE FROM not_notas " +
						"WHERE not_fecha = @fecha AND " +
						"alu_dni = @alu_dni AND mat_id = @mat_id";
					
					OleDbCommand eliminar = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					trans = conex.BeginTransaction();
					
					eliminar.Parameters.AddWithValue("@fecha",fecha);
					eliminar.Parameters.AddWithValue("@alu_dni",alu_dni);
					eliminar.Parameters.AddWithValue("@mat_id",mat_id);
					
					eliminar.ExecuteNonQuery();
					
					trans.Commit();
					
					eliminada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				eliminada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return eliminada;
		}
		
		public bool ModifyNota(int alu_dni_anterior, int mat_id_anterior, DateTime fecha_anterior, int alu_dni, int mat_id, int cur_id, int tie_id, DateTime fecha, string valor)
		{
			bool modificada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "UPDATE not_notas SET alu_dni = @alu_dni, mat_id = @mat_id, cur_id = @cur_id, " +
									"tie_id = @tie_id, not_fecha = @fecha, not_valor = @valor" +
									" WHERE not_fecha = @fecha_anterior AND alu_dni = @dni_anterior" +
									" AND mat_id = @mat_id_anterior";
					
					OleDbCommand modificar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					modificar.Parameters.AddWithValue("@alu_dni",alu_dni);
					modificar.Parameters.AddWithValue("@mat_id",mat_id);
					modificar.Parameters.AddWithValue("@cur_id",cur_id);
					modificar.Parameters.AddWithValue("@tie_id",tie_id);
					modificar.Parameters.AddWithValue("@fecha",fecha);
					modificar.Parameters.AddWithValue("@valor",valor);
					modificar.Parameters.AddWithValue("@fecha_anterior",fecha_anterior);
					modificar.Parameters.AddWithValue("@dni_anterior",alu_dni_anterior);
					modificar.Parameters.AddWithValue("@mat_id_anterior",mat_id_anterior);
					
					modificar.ExecuteNonQuery();
					
					trans.Commit();
					
					modificada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				modificada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return modificada;
		}
		
		public System.Data.DataTable FindAllNotas()
		{
			DataTable tablaNotas = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				tablaNotas = new DataTable();
				
				if (null != conex) {
					
					string sql = "SELECT alu_dni, alu_nombre,alu_apellido, mat_nombre, " +
									"cur_anio, cur_cuatrimestre, not_fecha, not_valor " +
									"FROM not_notas not, alu_alumnos alu, cur_cursadas cur, " +
									"mat_materias mat WHERE not.alu_dni = alu.alu_dni AND " +
									"not.cur_id = cur.cur_id AND not.mat_id = mat.mat_id";
					
					 OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);
					 
					 conex.Open();
					 
					 adaptador.Fill(tablaNotas);
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				tablaNotas = null;
				conex.Close();
			}
			
			return tablaNotas;
		}
		
		public System.Collections.Generic.List<Gestión_UTN.Entities.Nota> FindNotaByDni(int alu_dni)
		{
			List<Nota> notas = null;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					notas = new List<Nota>();
					
					string sql = "SELECT alu_dni, mat_id, cur_id, tie_id, not_fecha, not_valor " +
									"FROM not_notas not " +
									"WHERE alu_dni = @dni";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					 
					consulta.Parameters.AddWithValue("@dni",alu_dni);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read())
					{
						Nota not = new Nota(Convert.ToInt16(lector[0].ToString()),
						                    Convert.ToInt16(lector[1].ToString()),
						                    Convert.ToInt16(lector[2].ToString()),
											Convert.ToInt16(lector[3].ToString()),
											Convert.ToDateTime(lector[4].ToString()),
											lector[5].ToString());
						notas.Add(not);
						
						not = null;
					}
					
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				trans.Rollback();
				notas = null;
				conex.Close();
			}
			
			return notas;
		}
		
		public System.Collections.Generic.List<Gestión_UTN.Entities.Nota> FindNotaByMateria(int mat_id, int cur_id)
		{
			List<Nota> notas = null;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					notas = new List<Nota>();
					
					string sql = "SELECT alu_dni, mat_id, cur_id, tie_id, not_fecha, not_valor " +
									"FROM not_notas not " +
									"WHERE mat_id = @mat_id AND cur_id = @cur_id";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					 
					consulta.Parameters.AddWithValue("@mat_id",mat_id);
					consulta.Parameters.AddWithValue("@cur_id",cur_id);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read())
					{
						Nota not = new Nota(Convert.ToInt16(lector[0].ToString()),
						                    Convert.ToInt16(lector[1].ToString()),
						                    Convert.ToInt16(lector[2].ToString()),
											Convert.ToInt16(lector[3].ToString()),
											Convert.ToDateTime(lector[4].ToString()),
											lector[5].ToString());
						notas.Add(not);
						
						not = null;
					}
					
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				trans.Rollback();
				notas = null;
				conex.Close();
			}
			
			return notas;
		}
	}
}
